<?php
	error_reporting(E_ALL); 
	ini_set('display_errors',1);
	require_once "functions.php";

	
	$db = open_connection_pdo();
	$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );	# for error handling and displaying.
	
	( ! isset( $_SESSION ) ) and session_start();
	$question_types = array( "multiple_choice", "match_the_following" );
	
	$test_id = $_SESSION['t_id'];	
	$stmnt = $db->prepare( "SELECT test_id FROM test_details WHERE test_id=:test_id" );
	$stmnt->bindParam( ":test_id", $test_id );
	$stmnt->execute();
	
	$result = $stmnt->fetch( PDO::FETCH_ASSOC );
		
	if ( ! $result["test_id"] ) {
		echo "Invalid request. The test ID submitted by you isn't a valid one, you hacker buddy!";
	}
	
	$total_questions_available = get_total_questions_count($db, $test_id);
	
	if ( isset($_POST["multiple_choice"] ) ) {
		$question_type = "multiple_choice";
		
		for( $mc_q_num = 0; $mc_q_num < count( $_POST["multiple_choice"] ); $mc_q_num++ ) {			
			$question_title = $_POST["multiple_choice"][$mc_q_num]["question_title"];
			$q_identifier = $_POST["multiple_choice"][$mc_q_num]["q_identifier"];
			if ( $q_identifier !== "new" ) {
				$q_identifier = substr( $q_identifier,  9 );
				# Now, this is a question being edited. So, we delete all the data pertaining to this question
				# before adding new data, coz we don't know what is being edited. i.e is the q_title being edited?
				# of the answer options, or whatever!
				
				# REMEMBER to set the question_number to q_identifer sent from browser.
				# coz you are editing question, you don't need to assign a new question number.
								
				try {
					$stmnt = $db->prepare("DELETE FROM questions WHERE question_number = :question_number");
					$stmnt->bindParam( ":question_number", $q_identifier );					
					$stmnt->execute();
					
					$stmnt = $db->prepare("DELETE FROM multiple_choice_answers WHERE question_number = :question_number");
					$stmnt->bindParam( ":question_number", $q_identifier );					
					$stmnt->execute();					
					
					if ( $_POST["multiple_choice"][$mc_q_num]["delete_question"] == "y" ) {
						continue;
					}
					// i.e. if the question was flagged for deletion, you just wipe out the data.
					// and continue to next question.
					
				} catch( PDOException $e ) {
					echo "Failed to delete questions before updating it.";					
				}
				$question_number = $q_identifier;				
			} else {
				$question_occurs_at = $_POST["multiple_choice"][$mc_q_num]["place_at"];
				$question_number = $total_questions_available + $question_occurs_at;
			}
			
			
			# Now, just insert data.
			try {
				$stmnt = $db->prepare("INSERT INTO questions VALUES(:test_id, :question_number, :question_type, :question_title)" );
				$stmnt->bindParam(":test_id", $test_id);
				$stmnt->bindParam(":question_number", $question_number);
				$stmnt->bindParam(":question_title", $question_title, PDO::PARAM_STR);
				$stmnt->bindParam(":question_type", $question_type, PDO::PARAM_STR);				
				$stmnt->execute();
			} catch( PDOException $e ) {
				echo "Failed to insert MC question title " . $e->getmessage();
			}
			
			for ( $mc_answer_maker = 0; $mc_answer_maker < count( $_POST["multiple_choice"][$mc_q_num]["answer_options"] ); $mc_answer_maker++ ) {
				
				$choice_text = $_POST["multiple_choice"][$mc_q_num]["answer_options"][$mc_answer_maker];
				$is_right_answer = $_POST["multiple_choice"][$mc_q_num]["right_or_wrong"][$mc_answer_maker];
					
				try {
					$stmnt = $db->prepare("insert into multiple_choice_answers VALUES(:test_id, :question_number, :answer_choice_number, :choice_text, :is_right_answer)" );
					
					$stmnt->bindParam(":test_id", $test_id);
					$stmnt->bindParam(":question_number", $question_number);
					$stmnt->bindParam(":answer_choice_number", $mc_answer_maker);
					$stmnt->bindParam(":choice_text", $choice_text, PDO::PARAM_STR);
					$stmnt->bindParam(":is_right_answer", $is_right_answer);
					
					$result = $stmnt->execute();					
				} catch( PDOException $e ) {
						echo "Failed to insert questions " . $e->getMessage();
				}
			}
		}
	}
	
	
	if ( isset($_POST["match_the_following"] ) ) {
		$question_type = "match_the_following";
		
		
		for( $mtf_q_num = 0; $mtf_q_num < count( $_POST["match_the_following"] ); $mtf_q_num++ ) {
			$q_identifier = $_POST["match_the_following"][$mtf_q_num]["q_identifier"];
			$question_title = $_POST["match_the_following"][$mtf_q_num]["question_title"];			
			
			if ( $q_identifier !== "new" ) {
				# Now, this is a question being edited. So, we delete all the data pertaining to this question
				# before adding new data, coz we don't know what is being edited. i.e is the q_title being edited?
				# of the answer options, or whatever!
				
				# REMEMBER to set the question_number to q_identifer sent from browser.
				# coz you are editing question, you don't need to assign a new question number.
				$q_identifier = substr( $q_identifier,  9 );
				try {
					$stmnt = $db->prepare("DELETE FROM questions WHERE question_number = :question_number");
					$stmnt->bindParam(":question_number", $q_identifier );
					$stmnt->execute();
					
					$stmnt = $db->prepare("DELETE FROM match_following_answers WHERE question_id = :question_number");
					$stmnt->bindParam( ":question_number", $q_identifier );
					$stmnt->execute();
					
				} catch ( PDOException $e ) {
					echo "Failed to delete old data from match_following_answers: " . $e->getMessage();
				}				
			
				if ( $_POST["match_the_following"][$mtf_q_num]["delete_question"] == "y" ) {
					continue;
				}
			
				// i.e. if the question was flagged for deletion, you just wipe out the data.
				// and continue to next question.
				
				$question_number = $q_identifier;				
			} else {
				$question_occurs_at = $_POST["match_the_following"][$mtf_q_num]["place_at"];
				$question_number = $total_questions_available + $question_occurs_at;
			}
			
			try {
				$stmnt = $db->prepare("INSERT INTO questions VALUES(:test_id, :question_number, :question_type, :question_title)");
				$stmnt->bindParam(":test_id", $test_id);
				$stmnt->bindParam(":question_number", $question_number);
				$stmnt->bindParam(":question_title", $question_title, PDO::PARAM_STR);
				$stmnt->bindParam(":question_type", $question_type, PDO::PARAM_STR);				
				$stmnt->execute();
			} catch( PDOException $e ) {
				echo "Failed to insert question title: match the following: " . $e->getMessage();
			}
			for ( $mtf_answer_maker = 0; $mtf_answer_maker < count( $_POST["match_the_following"][$mtf_q_num]["mtf_mapping"] ); $mtf_answer_maker++ ) {
				$left_choice_text = $_POST["match_the_following"][$mtf_q_num]["mtf_mapping"][$mtf_answer_maker][0];
				$right_choice_text = $_POST["match_the_following"][$mtf_q_num]["mtf_mapping"][$mtf_answer_maker][1];
				try {
					$stmnt = $db->prepare("insert into match_following_answers VALUES(:test_id, :question_number, :choice_number, :left_choice_text, :right_choice_text)" );
				
					$stmnt->bindParam(":test_id", $test_id);
					$stmnt->bindParam(":question_number", $question_number);
					$stmnt->bindParam(":choice_number", $mtf_answer_maker);
					$stmnt->bindParam(":left_choice_text", $left_choice_text, PDO::PARAM_STR);
					$stmnt->bindParam(":right_choice_text", $right_choice_text, PDO::PARAM_STR);
				
					
					$stmnt->execute();
					
				} catch( PDOException $e ) {
					echo "Failed to insert match the following question: " . $e->getMessage();
				}
			}
			
		}
	}
	
?>


